package cn.exrick.xboot.modules.Information.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.utils.SecurityUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.Information.entity.Information;
import cn.exrick.xboot.modules.Information.service.IInformationService;
import cn.exrick.xboot.modules.base.entity.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author ZhangWei
 */
@Slf4j
@RestController
@Api(description = "信息管理管理接口")
@RequestMapping("/xboot/information")
@Transactional
public class InformationController {

    @Autowired
    private IInformationService iInformationService;

    @Autowired
    private SecurityUtil securityUtil;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Information> get(@PathVariable Long id){

        Information information = iInformationService.getById(id);
        return new ResultUtil<Information>().setData(information);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<Information>> getAll(){

        List<Information> list = iInformationService.list();
        return new ResultUtil<List<Information>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<Information>> getByPage(@ModelAttribute PageVo page){

        IPage<Information> data = iInformationService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<Information>>().setData(data);
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<IPage<Information>> getByCondition(@ModelAttribute Information information, @ModelAttribute PageVo page){

        QueryWrapper<Information> qw = new QueryWrapper<Information>();
        if(information.getTitle() != null && !"".equals(information.getTitle())){
            qw.like("title",information.getTitle());
        }
        if(information.getContent() != null && !"".equals(information.getContent())){
            qw.like("content",information.getContent());
        }
        if(information.getType() != null && !"".equals(information.getType())){
            qw.eq("type",information.getType());
        }
        if(information.getFlag() != null && !"".equals(information.getFlag())){
            qw.eq("flag",information.getFlag());
        }
        IPage<Information> data = iInformationService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<Information>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "更新数据")
    public Result<Information> saveOrUpdate(@ModelAttribute Information information){

        User user = securityUtil.getCurrUser();
        information.setCreateBy(user.getUsername());
        Date date = new Date();
        information.setCreateTime(date);
        if(iInformationService.saveOrUpdate(information)){
            return new ResultUtil<Information>().setData(information);
        }
        return new ResultUtil<Information>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加消息")
    public Result<Information> add(@ModelAttribute Information information){
        Date date = new Date();
        information.setCreateTime(date);
        if(iInformationService.save(information)){
            return new ResultUtil<Information>().setData(information);
        }
        return new ResultUtil<Information>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable Long[] ids){

        for(Long id : ids){
            iInformationService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
}
